#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUFFER_SIZE  32 //不允许使用魔数

#define SIZE  3



//字节对齐

typedef struct StuInfo  //typedef取别名
{
    int age;
    char sex;
    #if 0
    char name[BUFFER_SIZE];
    #endif
    char *address;  //结构体有指针的一定要分配空间

    char height;
} StuInfo;  //

/*值传递：浪费资源*/
/*地址传递：节省资源*/

#if 0
int printStruct(struct StuInfo *pInfo)
{
    
    int ret = 0;
    if(pInfo == NULL)
    {
        perror("param is NULL");
        exit(-1);

    }

   
    
    int len = sizeof(pInfo);
    printf("=========len:%d\n",len);

     for(int idx = 0; idx < size; idx++)
    {
    //结构体指针取值使用的是 ->
    printf("age:%d\t, sex:%c\t, name:%s\t, height:%d\n", pInfo[idx]->age, pInfo[idx]->sex, pInfo[idx]->name, pInfo[idx]->height);
    }
    return ret;
}
#endif

int main()
{
    #if 0
    /*结构体的基本使用*/
    struct StuInfo stu1;
    
    int len = sizeof(stu1);
    printf("len:%d\n", len);

    len = sizeof(struct StuInfo);
    printf("len:%d\n", len);

    //清空脏数据
    memset(&stu1, 0, sizeof(stu1));

    //赋值
    //数组名是数组的首地址,常量不可以更改
    //stu1.name = "zhangsan";
    strcpy(stu1.name, "zhangsan");
    stu1.age = 20;
    stu1.height = 60;
    stu1.sex = 'm';


    //写一个函数打印该结构体
    printStruct(&stu1);

    #endif

    #if 0
    StuInfo info[SIZE];
    int len = sizeof(info);
    printf("len:%d\n", len);

    /*清楚脏数据*/
    memset(info, 0, sizeof(info));

    
    //info[0].name = "lisi" //此写法不正确

       
    info[0].age = 20;
    strcpy(info[0].name,"lise");
    info[0].sex = 'm';
    info[0].height = 170;

    info[1].age = 40;
    strcpy(info[1].name,"zhangsan");
    info[1].sex = 'm';
    info[1].height = 180;


    info[1].age = 50;
    strcpy(info[0].name,"wangwu");
    info[2].sex = 'm';
    info[2].height = 165;
    
   
    for(int idx = 0; idx < SIZE; idx++)
        {
            printStruct(&(info[idx]));

        }


        
        printStruct(info, sizeof(info) / sizeof());

    #endif
    #if 1
        StuInfo stu;
        memset(&stu, 0, sizeof(stu));

        //stu.address = "jiangshuancheng";


        /*结构体有指针的一定要分配空间*/


        stu.address = (char*)malloc(sizeof(char) * BUFFER_SIZE);
        
        /*判空*/
        if(stu.address == NULL)
        {
            perror("malloc error");
            exit -1;
        }
        strcpy(stu.address, "jiangsuyancheng");
        stu.age = 22;
        stu.height = 65;
        stu.sex = 'm';
        printf("age:%d\t, sex:%c\t, name:%s\t, height:%d\n", stu.age, stu.sex, stu.address, stu.height);


        free(stu.address);
    #endif


    return 0;

}